package com.fansl.allround.upms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fansl.allround.common.core.constant.CacheConstants;
import com.fansl.allround.upms.api.dto.UserMenuDTO;
import com.fansl.allround.upms.api.entity.SysUserMenu;
import com.fansl.allround.upms.mapper.SysUserMenuMapper;
import com.fansl.allround.upms.service.SysUserMenuService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author fansl
 * @Description: 用户菜单表 服务实现类
 * @date 2019/8/26 15:57
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserMenuServiceImpl extends ServiceImpl<SysUserMenuMapper, SysUserMenu> implements SysUserMenuService {

    /**
     * 更新用户自定义菜单,并清空角色菜单缓存
     *
     * @param userMenuDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(evict = {
            @CacheEvict(value = CacheConstants.CACHE_MENU_DETAIL, key = "#p0.userId+'" + CacheConstants.CACHE_USER_MENU_SUFFIX + "'"),
            @CacheEvict(value = CacheConstants.CACHE_MENU_DETAIL, key = "#p0.userId+'" + CacheConstants.CACHE_USER_PERMISSION_SUFFIX + "'")
    })
    public Boolean updateUserMenu(UserMenuDTO userMenuDTO) {
        Set<SysUserMenu> plusUserMenuList = Arrays.stream(userMenuDTO.getPlusMenuIds().split(","))
                .filter(menuId -> StrUtil.isNotBlank(menuId))
                .map(menuId -> {
                    SysUserMenu sysUserMenu = new SysUserMenu();
                    sysUserMenu.setMenuId(Integer.parseInt(menuId));
                    sysUserMenu.setUserId(userMenuDTO.getUserId());
                    sysUserMenu.setType(1);
                    return sysUserMenu;
                })
                .collect(Collectors.toSet());
        Set<SysUserMenu> reduceUserMenuList = Arrays.stream(userMenuDTO.getReduceMenuIds().split(","))
                .filter(menuId -> StrUtil.isNotBlank(menuId))
                .map(menuId -> {
                    SysUserMenu sysUserMenu = new SysUserMenu();
                    sysUserMenu.setMenuId(Integer.parseInt(menuId));
                    sysUserMenu.setUserId(userMenuDTO.getUserId());
                    sysUserMenu.setType(2);
                    return sysUserMenu;
                })
                .collect(Collectors.toSet());
        List<SysUserMenu> sysUserMenus = new ArrayList<>();
        sysUserMenus.addAll(plusUserMenuList);
        sysUserMenus.addAll(reduceUserMenuList);
        //先删除原来的
        this.remove(Wrappers.<SysUserMenu>query().lambda()
                .eq(SysUserMenu::getUserId, userMenuDTO.getUserId()));
        //再新增数据
        return this.saveBatch(sysUserMenus);
    }
}
